package service.impl;

import common.Constants.MapKey;
import common.Constants.ServiceConstant;
import common.Result;
import dao.*;
import dataBean.*;
import org.springframework.beans.factory.annotation.Autowired;
import service.QueryService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxl on 2015/10/30.
 */
public class QueryServiceImpl implements QueryService{

    @Autowired
    private UserDao userDao ;

    @Autowired
    private UserInfoDao userInfoDao ;

    @Autowired
    private CourseInfoDao courseInfoDao ;

    @Autowired
    private ScoresDao scoresDao ;

    @Autowired
    private ScoreInfoDao scoreInfoDao ;


    @Override
    public Result queryUserList(int startRow, int pageSize , int level ) {

        //not support paging now

        Result result = new Result() ;

        try {

            List<UserInfoDo> userInfoDos ;

            if( level == 0 ){

                 userInfoDos = userInfoDao.selectAllUser() ;

            }else if(level > 3 || level < 0 ){

                result.setSucces(false);
                result.setError(ServiceConstant.NO_THIS_LEVEL);
                return result ;

            }else{

                userInfoDos = userInfoDao.selectUserByLevel(level) ;

            }

            if( userInfoDos != null ){

                result.setSucces(true);
                result.addParam(MapKey.USER_LIST,userInfoDos);

            }else {

                result.setSucces(false) ;
                result.setError(ServiceConstant.USER_EXIST_ERROR);

            }


        }catch (Exception e){

            result.setSucces(false);
            result.setError(ServiceConstant.USER_QUERY_ERROR);

        }


        return result;
    }


    @Override
    public Result queryCourseList(int startRow, int pageSize , int level , String username ) {

        Result result = new Result() ;

        try {

            if( level == ServiceConstant.ADMIN_LEVEL ){

                List<CourseInfoDo> courseInfoDos = courseInfoDao.selectAllCourse() ;

                if( courseInfoDos != null ){

                    List<UserInfoDo> userInfoDos = new ArrayList<UserInfoDo>() ;
                    List<Integer> ids = new ArrayList<Integer>() ;
                    for( CourseInfoDo courseInfoDo :courseInfoDos){

                        courseInfoDo.setTeacherId(courseInfoDao.selectTeacherId(courseInfoDo.getId()));
                        if( !ids.contains(courseInfoDo.getTeacherId())){

                            ids.add(courseInfoDo.getTeacherId()) ;

                        }
                    }

                    for ( int i = 0 ; i < ids.size() ; i++){

                        UserInfoDo userInfoDo = userInfoDao.selectUserInfoById(ids.get(i)) ;
                        userInfoDos.add(userInfoDo) ;

                    }

                    result.setSucces(true);
                    result.addParam(MapKey.USER_LIST,userInfoDos);
                    result.addParam(MapKey.COURSE_LIST,courseInfoDos);

                }else {

                    result.setSucces(false);
                    result.setError(ServiceConstant.NO_SUCH_COURSE);

                }

            }else  if( level == ServiceConstant.TEACHER_LEVEL ){

                List<CourseInfoDo> courseInfoDos = courseInfoDao.selectCourseByTeacherUserName(username) ;

                String name = userInfoDao.selectUserInfoByName(username).getName() ;

                result.setSucces(true);
                result.addParam(MapKey.COURSE_LIST,courseInfoDos);
                result.addParam(MapKey.USER_NAME,name);

            }else  if( level == ServiceConstant.STUDENT_LEVEL ){

                List<CourseInfoDo> courseInfoDos = courseInfoDao.selectCourseByStudentUserName(username) ;

                List<UserInfoDo> userInfoDos = new ArrayList<UserInfoDo>() ;

                for( CourseInfoDo courseInfoDo :courseInfoDos ){

                    int id = courseInfoDao.selectTeacherId(courseInfoDo.getId()) ;

                    courseInfoDo.setId(id);
                    UserInfoDo userInfoDo = userInfoDao.selectUserInfoById(id) ;

                    userInfoDos.add(userInfoDo) ;

                }

                result.setSucces(true);
                result.addParam(MapKey.COURSE_LIST,courseInfoDos);
                result.addParam(MapKey.USER_NAME,userInfoDos);

            }
            else {

                result.setSucces(false);
                result.setError(ServiceConstant.NO_THIS_LEVEL);

            }

        }catch (Exception e){

            result.setSucces(false);
            result.setError(ServiceConstant.COURSE_QUERY_ERROR);

        }


        return result;
    }

    @Override
    public Result queryStudentList() {
        Result result = new Result();

        try {
            List<UserInfoDo> userInfoDos = userInfoDao.selectUserByLevel(ServiceConstant.STUDENT_LEVEL);

            result.setSucces(true);
            result.addParam(MapKey.STUDENT_LIST, userInfoDos);
        } catch (Exception e) {
            result.setSucces(false);
            result.setError(ServiceConstant.QUERY_STUDENT_LIST_ERROR);
        }

        return result;
    }

    @Override
    public Result queryStudentList(int startRow, int pageSize, String username) {

        Result result = new Result() ;

        try {

            List<StudentCourseDo> studentCourseDos = new ArrayList<StudentCourseDo>() ;

            List<CourseInfoDo> courseInfoDos = courseInfoDao.selectIdsByUsername(username) ;

            for(CourseInfoDo courseInfoDo:courseInfoDos ){

                List<UserInfoDo> userInfoDos = scoresDao.selectUserInfoByCourseId(courseInfoDo.getId()) ;

                    for( UserInfoDo userInfoDo : userInfoDos ){

                        StudentCourseDo studentCourseDo = new StudentCourseDo() ;

                        studentCourseDo.setName(userInfoDo.getName());
                        studentCourseDo.setUsername(userInfoDo.getUsername());
                        studentCourseDo.setCourseName(courseInfoDo.getName());

                        ScoresDo scoresDo = new ScoresDo() ;

                        scoresDo.setCourseID(courseInfoDo.getId());
                        scoresDo.setUserID(userInfoDo.getId());

                        int scoreId = scoresDao.selectScoresByKey(scoresDo)  ;

                        if( scoreId == 0 ){

                            studentCourseDo.setOverall(-1);

                        }else {

                            studentCourseDo.setOverall(scoreInfoDao.selectScoreInfoById(scoreId).getOverall());

                        }

                        studentCourseDo.setId(courseInfoDo.getId());
                        studentCourseDo.setStudentId(userInfoDo.getId());

                        studentCourseDos.add(studentCourseDo) ;

                    }

            }

            result.setSucces(true);
            result.addParam(MapKey.COURSE_LIST,studentCourseDos);


        }catch (Exception e){

            result.setSucces(false);
            result.setError(ServiceConstant.QUERY_STUDENT_COURSE_ERROR);

            System.out.println(e.toString());

        }

        return result;
    }

    @Override
    public Result queryScore(int startRow, int pageSize, String username) {

        Result result = new Result() ;

        try {

            List<CourseInfoDo> courseInfoDos = new ArrayList<CourseInfoDo>() ;

            List<ScoreInfoDo> scoreInfoDos = new ArrayList<ScoreInfoDo>() ;

            List<Integer> scoreIds = scoresDao.selectScoreIdByUsername(username) ;

            for( int i = 0 ; i < scoreIds.size() ; i++){

                CourseInfoDo courseInfoDo = courseInfoDao.selectCourseByScoreId(scoreIds.get(i)) ;
                ScoreInfoDo scoreInfoDo = scoreInfoDao.selectScoreInfoByCourseId(scoreIds.get(i)) ;

                if( scoreInfoDo == null ){

                    scoreInfoDo = new ScoreInfoDo() ;

                }

                courseInfoDo.setId(scoreIds.get(i));
                scoreInfoDo.setId(scoreIds.get(i));

                courseInfoDos.add(courseInfoDo) ;
                scoreInfoDos.add(scoreInfoDo) ;

            }

            result.setSucces(true);
            result.addParam(MapKey.COURSE_LIST,courseInfoDos);
            result.addParam(MapKey.SCORE_LIST,scoreInfoDos);

        }catch (Exception e){

            result.setSucces(false);
            result.setError(ServiceConstant.QUERY_SCORE_ERROR);

        }


        return result;
    }

    @Override
    public Result queryScore(int startRow, int pageSize, ScoresDo scoresDo) {

        Result result = new Result() ;

        try{

            int scoreId = scoresDao.selectScoresByKey(scoresDo) ;

            if( scoreId == 0 ){

                result.setSucces(true);
                result.addParam(MapKey.SCORE_LIST,null);

            }else {

                ScoreInfoDo scoreInfoDo = scoreInfoDao.selectScoreInfoById(scoreId) ;

                result.setSucces(true);
                result.addParam(MapKey.SCORE_LIST,scoreInfoDo);

            }


        }catch (Exception e){

            result.setSucces(false);
            result.setError(ServiceConstant.QUERY_SCORE_ERROR);

        }

        return result;
    }

    @Override
    public Result queryCourseTeacherList(int level, String courseName) {
        Result result = new Result();

        try {
            if (level == ServiceConstant.ADMIN_LEVEL) {
                List<String> teacherList = userInfoDao.selectTeacherOnCourse(courseName);

                if (teacherList != null) {
                    result.setSucces(true);
                    result.addParam(MapKey.TEACHER_LIST_ON_COURSE, teacherList);
                }
            }
        } catch (Exception e) {

            result.setSucces(false);
            result.setError(ServiceConstant.QUERY_TEACHER_ON_COURSE_ERROR);
        }

        return result;
    }
}
